﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class cms_Admin_Danhgia_u_Tudanhgia : System.Web.UI.UserControl
{
    #region[Khai bao]
    string uID = "";
    string p = "";
    string ps = "";
    static Double MaxPoint = 0;
    static Double Diemchia = 0;
    static Double Total = 0;
    static Double PointNV = 0;
    static Double PointLD = 0;
    #endregion
    #region[Page_Load]
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["uid"] != null) { uID = Session["uid"].ToString(); }
        if (Session["p"] != null) { p = Session["p"].ToString(); }
        if (Session["ps"] != null) { ps = Session["ps"].ToString(); }
        if (!IsPostBack) 
        {
            List<tbConfigDATA> list = tbConfigDB.tbConfig_GetAll();
            List<tbAssessmentDATA> list1 = tbAssessmentDB.tbAssessment_GetByuID_DaDanhGia(uID);
            if (list.Count > 0)
            {
                MaxPoint = Convert.ToDouble(list[0].coMaxPoint);
                Total = Convert.ToDouble(list[0].coTotal);
                PointNV = Convert.ToDouble(list[0].coPointNV);
                PointLD = Convert.ToDouble(list[0].coPointLD);
                if (list[0].coStatus == "0") { lbtCapnhat.Visible = false; lblLoi.Text = "Hệ thống đánh giá đã bị đóng !!"; } else { lbtCapnhat.Visible = true; }
            }
            if (list1.Count > 0)
            {
                lbtCapnhat.Visible = false; lblLoi.Text = "Hệ thống đã ghi nhận đánh giá cấp trên của bạn, bạn không được phép đánh giá tháng này nữa !!";
            }
            else
            {
                lbtCapnhat.Visible = true;
            }
            list.Clear();
            list = null;
            list1.Clear();
            list1 = null;
            Viewtieuchi();
        }
    }
    #endregion
    #region[Viewtieuchi]
    void Viewtieuchi()
    {
        List<tbCriteriaDATA> list = tbCriteriaDB.tbCriteria_GetBypsID(ps);
        rptDanhsach.DataSource = list;
        rptDanhsach.DataBind();
        for (int i = 0; i < list.Count; i++)
        {
            DropDownList drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
            Label lblorder = (Label)rptDanhsach.Items[i].FindControl("lblorder");
            int k = (i + 1);
            lblorder.Text = k.ToString();
            drlDiem.Items.Clear();
            for (int j = 1; j < MaxPoint + 1; j++)
            {
                drlDiem.Items.Add(j.ToString());
            }
        }
        MaxPoint = MaxPoint * list.Count;
        Diemchia = Total / MaxPoint;
        list.Clear();
        list = null;
    }
    #endregion
    #region[Danhgia]
    protected void lbtCapnhat_Click(object sender, EventArgs e)
    {
        int month = DateTime.Now.Month - 1;
        string assDateBegin = month.ToString() + "/" + "01" + "/" + DateTime.Now.Year.ToString();
        DateTime assDateEnd = vmmsclass.vmmsdate.GetLastDayOfMonth(Convert.ToDateTime(assDateBegin.ToString()));
        if (rptDanhsach.Items.Count > 0)
        {
            List<tbAssessmentDATA> list1 = tbAssessmentDB.tbAssessment_GetByCheckuID(uID);
            if (list1.Count == 0)
            {
                tbAssessmentDATA ObjtbAssessmentDATA = new tbAssessmentDATA();
                ObjtbAssessmentDATA.assDateBegin = assDateBegin;
                ObjtbAssessmentDATA.assDateEnd = assDateEnd.ToString();
                ObjtbAssessmentDATA.assPoint = "0";
                ObjtbAssessmentDATA.assPointLD = "0";
                ObjtbAssessmentDATA.assPointLD_1 = "0";
                ObjtbAssessmentDATA.assTotalpoint = "0";
                ObjtbAssessmentDATA.uID = uID;
                ObjtbAssessmentDATA.uIDLD = "-1";
                ObjtbAssessmentDATA.uIDLD_1 = "-1";
                try
                {
                    if (tbAssessmentDB.tbAssessment_Add(ObjtbAssessmentDATA))
                    {
                        List<tbAssessmentDATA> list = tbAssessmentDB.tbAssessment_GetByMax();
                        DropDownList drlDiem;
                        Label lblID;
                        for (int i = 0; i < rptDanhsach.Items.Count; i++)
                        {
                            drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                            lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                            tbAssessmentDetailDATA ObjtbAssessmentDetailDATA = new tbAssessmentDetailDATA();
                            ObjtbAssessmentDetailDATA.assId = list[0].assId;
                            ObjtbAssessmentDetailDATA.criId = lblID.Text;
                            ObjtbAssessmentDetailDATA.adePoint = drlDiem.SelectedValue;
                            ObjtbAssessmentDetailDATA.adePointPercent = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointNV) / Total);
                            ObjtbAssessmentDetailDATA.adePointLD = "0";
                            ObjtbAssessmentDetailDATA.adePointPercentLD = "0";
                            ObjtbAssessmentDetailDATA.adePointLD_1 = "0";
                            ObjtbAssessmentDetailDATA.adePointPercentLD_1 = "0";
                            ObjtbAssessmentDetailDATA.adeTotalpointPercent = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointNV) / Total);
                            ObjtbAssessmentDetailDATA.adeType = "0";
                            if (tbAssessmentDetailDB.tbAssessmentDetail_Add(ObjtbAssessmentDetailDATA))
                            {
                                lblThongbao.Text = "Đánh giá công việc thành công !!";
                            }
                        }
                        list.Clear();
                        list = null;
                    }
                }
                catch { lblLoi.Text = "Lỗi dữ liệu nhập vào !!"; return; }
            }
            else
            {
                list1[0].assId = list1[0].assId;
                list1[0].uID = uID;
                list1[0].uIDLD = list1[0].uIDLD;
                tbAssessmentDB.tbAssessment_Update_UserID(list1[0]);

                List<tbAssessmentDetailDATA> listde = tbAssessmentDetailDB.tbAssessmentDetail_GetByassId(list1[0].assId);
                if (listde.Count > 0)
                {
                    DropDownList drlDiem;
                    Label lblID;
                    for (int i = 0; i < rptDanhsach.Items.Count; i++)
                    {
                        drlDiem = (DropDownList)rptDanhsach.Items[i].FindControl("drlDiem");
                        lblID = (Label)rptDanhsach.Items[i].FindControl("lblID");
                        for (int j = 0; j < listde.Count; j++)
                        {
                            if (lblID.Text == listde[j].criId)
                            {
                                List<tbAssessmentDetailDATA> listID = tbAssessmentDetailDB.tbAssessmentDetail_GetByID(listde[j].adeId);
                                if (listID.Count > 0)
                                {
                                    listID[0].adeId = listde[j].adeId;
                                    listID[0].adePoint = drlDiem.SelectedValue;
                                    listID[0].adePointPercent = Convert.ToString((Convert.ToDouble(drlDiem.SelectedValue) * Diemchia * PointNV) / Total);
                                    if (tbAssessmentDetailDB.tbAssessmentDetail_Update_PointuID(listID[0]))
                                    { lblLoi.Text = "Đánh giá công việc thành công !!"; }
                                }
                                listID.Clear();
                                listID = null;
                            }
                        }
                    }
                }
                listde.Clear();
                listde = null;
            }
        }
    }
    #endregion
}
